package com.wanji.single_point.controller;


import com.wanji.single_point.entity.Alarm;
import com.wanji.single_point.pojo.AlarmResult;
import com.wanji.single_point.pojo.PutParam;
import com.wanji.single_point.service.AlarmService;
import com.wanji.single_point.utils.ResultUtil;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *  交通事件感知能力  Alarm
 * </p>
 *
 * @author xinyingjie
 * @since 2022-07-14
 */
@CrossOrigin
@RestController
@RequestMapping("/alarm")
public class AlarmController {

    @Autowired
    private AlarmService alarmService;


    /**
     * 存放用户信息，key:userId,value:用户创建时间
     */
    private static Map<String, PutParam> user = new ConcurrentHashMap<>();



    /**
     * 接收okhttp发送工具发送的真值数据，如果有用户访问,将数据传入数据处理逻辑
     * @param jsonParam
     * @return
     */
    @PostMapping("/getReal")
    public ResultUtil getReal(@RequestBody String jsonParam) {

        Alarm[] bodiesReal = (Alarm[]) JSONArray.toArray(JSONArray.fromObject(jsonParam), Alarm.class);


        if (user.isEmpty()){
            return ResultUtil.fail(200,"数据传输成功,没有用户访问数据");
        }
        for (String userId : user.keySet()) {
            PutParam param = user.get(userId);
            alarmService.putData(userId, bodiesReal, null, param);
        }
        return ResultUtil.success(200,"数据传输成功","ok");

    }

    /**
     * 接收okhttp发送工具发送的测试数据，如果有用户访问,将数据传入数据处理逻辑
     * @param jsonParam
     * @return
     */
    @PostMapping("/getTest")
    public ResultUtil getTest(@RequestBody String jsonParam) {
        Alarm[] bodiesTest = (Alarm[]) JSONArray.toArray(JSONArray.fromObject(jsonParam), Alarm.class);

        if (user.isEmpty()){

            return ResultUtil.fail(200,"数据传输成功,没有用户访问数据");
        }
        for (String userId : user.keySet()) {
            PutParam param = user.get(userId);
            alarmService.putData(userId, null, bodiesTest,param);
        }
        return ResultUtil.success(200,"数据传输成功","ok");

    }

    @PostMapping("/putParam")
    public ResultUtil putParam(@RequestParam(value = "deviceId", defaultValue = "") String deviceId,
                               @RequestParam(value = "realCode", defaultValue = "") String realCode,
                               @RequestParam(value = "testCode", defaultValue = "") String testCode,
                               @RequestParam(value = "beginTime", defaultValue = "") Long beginTime,
                               @RequestParam(value = "endTime", defaultValue = "") Long endTime) {

        PutParam param = alarmService.putParamer(realCode, testCode, beginTime, endTime);
        String userId = deviceId + realCode + testCode;
        user.put(userId, param);

        return ResultUtil.success(200, "返回结果成功", "ok");
    }


    /**
     * 根据userId获取返回结果
     * @return
     * @throws Exception
     */
    //    @GetMapping("/getResult/{userId}")  @PathVariable String userId
    @PostMapping("/getResult")

    public ResultUtil getResult(@RequestParam(value = "deviceId", defaultValue = "") String deviceId,
                                @RequestParam(value = "realCode", defaultValue = "") String realCode,
                                @RequestParam(value = "testCode", defaultValue = "") String testCode) throws Exception {
        String userId = deviceId + realCode + testCode;
        AlarmResult result = alarmService.getResult(userId);
        if (result == null) {
            return ResultUtil.fail(400, "测试时间到或者参数错误");
        }
        return ResultUtil.success(200, "返回结果成功", result);
    }


    /**
     * 用户推出登录
     * @param userId
     * @return
     */
    @PostMapping("/signout/{userId}")
    public ResultUtil signOut(@PathVariable String userId) {
        user.remove(userId);
        alarmService.deleteCache(userId);
        return ResultUtil.success(200, "退出成功", "ok");
    }

}
